﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Microsoft.Web.Mvc;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class RoomTypeController : Controller
    {
        //
        // GET: /RoomType/
        private readonly IRoomTypeRepository roomTypeRepository;
        private readonly IUnitOfWork unitOfWork;
        public RoomTypeController(IRoomTypeRepository roomTypeRepository, IUnitOfWork unitOfWork)
        {
            this.roomTypeRepository = roomTypeRepository;
            this.unitOfWork = unitOfWork;
        }
      
         private IList<RoomTypeInfoModel> GetRoomType()
        {
            var roomType = roomTypeRepository.GetAll();
            IList<RoomTypeInfoModel> roomTypeInfoModels = (from r in roomType
                                                     select new RoomTypeInfoModel
                                                                {
                                                                    ID = r.ID,
                                                                    Name = r.Name,
                                                                    Price = r.Price,
                                                                    Remark =r.Remark
                                                                }
                                                    ).ToList() ;
            return roomTypeInfoModels;
        }

        public ActionResult ShowAllRoomType()
        {
           return View(GetRoomType());
        }

        [GridAction]
        public ActionResult TeletrikAjax()
        {
            return View(new GridModel(GetRoomType()));
        }

        [HttpPost]
        [GridAction]
        public ActionResult AddRoomType()
        {
            var roomTypeInfoModel = new RoomTypeInfoModel();
            if (TryUpdateModel(roomTypeInfoModel))
            {
                var roomType = new RoomType();
                ModelCopier.CopyModel(roomTypeInfoModel, roomType);
                roomTypeRepository.Add(roomType);
                unitOfWork.Commit();
            }
            return View(new GridModel(GetRoomType()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult DeleteRoomType(int Id)
        {
            var model = roomTypeRepository.Get(x => x.ID == Id);
            if(model!=null)
            {
                 roomTypeRepository.Delete(model);
                 unitOfWork.Commit();
            }
           
            return View(new GridModel(GetRoomType()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult UpdateRoomType(int Id)
        {
            var model = roomTypeRepository.Get(x => x.ID == Id);
            if (TryUpdateModel(model))
            {
                roomTypeRepository.Update(model);
                unitOfWork.Commit();
            }

            return View(new GridModel(GetRoomType()));
        }
    }
}
